package xkp.lesson.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xkp.lesson.entity.AccountInfo;
import xkp.lesson.mapper.AccountInfoMapper;

import java.math.BigDecimal;

@Service
public class AccountInfoService {
    @Autowired
    private AccountInfoMapper accountInfoMapper;

    @Transactional
    public void transMoney(BigDecimal srcId,BigDecimal descId, BigDecimal money){

        AccountInfo srcInfo = accountInfoMapper.selectByPrimaryKey(srcId);
        AccountInfo descInfo = accountInfoMapper.selectByPrimaryKey(descId);
        srcInfo.setAccountMoney(srcInfo.getAccountMoney().subtract(money));
        descInfo.setAccountMoney(descInfo.getAccountMoney().add(money));


        accountInfoMapper.updateByPrimaryKeySelective(descInfo);
        accountInfoMapper.updateByPrimaryKeySelective(srcInfo);
    }
}
